package util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class Utilities {

    public static String getUserId() {
	long time = System.currentTimeMillis();
	StringBuffer buffer = new StringBuffer();
	buffer.append(time);
	Random random = new Random();
	for (int i = 1; i <= 4; i++) {
	    buffer.append(random.nextInt(10));
	}
	return buffer.substring(buffer.length() - 10);

    }

    public static String getCourseName(String idString) {
	int num = Integer.parseInt(idString);
	String coursName;
	switch (num) {
	case 1:
	    coursName = "语文";
	    break;
	case 2:
	    coursName = "数学";
	    break;
	case 3:
	    coursName = "英语";
	    break;
	case 4:
	    coursName = "物理";
	    break;
	case 5:
	    coursName = "化学";
	    break;
	case 6:
	    coursName = "生物";
	    break;
	case 7:
	    coursName = "历史";
	    break;
	case 8:
	    coursName = "政治";
	    break;
	case 9:
	    coursName = "地理";
	    break;
	default:
	    coursName = "";
	    break;
	}
	return coursName;
    }

    public static String getGradeName(String idString) {
	int num = Integer.parseInt(idString);
	String coursName;
	switch (num) {
	case 1:
	    coursName = "初中一年级";
	    break;
	case 2:
	    coursName = "初中二年级";
	    break;
	case 3:
	    coursName = "初中三年级";
	    break;
	case 4:
	    coursName = "高中一年级";
	    break;
	case 5:
	    coursName = "高中二年级";
	    break;
	case 6:
	    coursName = "高中三年级";
	    break;
	default:
	    coursName = "";
	    break;
	}
	return coursName;
    }

    public static String getTeacherId(Date date) {
	if (date == null) {
	    date = new Date();
	}
	StringBuffer buffer = new StringBuffer("tea");
	DateFormat dFormat = new SimpleDateFormat("yyMMdd");
	buffer.append(dFormat.format(date));
	int num = (int) (Math.random() * 10);
	buffer.append(num);
	return buffer.toString();
    }

    public static String makeImageName() {
	StringBuffer buffer = new StringBuffer();
	DateFormat dFormat = new SimpleDateFormat("yyMMddhhMMSSS");
	buffer.append(dFormat.format(new Date()));
	return buffer.toString();
    }

    public static int isRight(String[] givenAnswer, String answer) {
	if (givenAnswer.length >= 1) {
	    if (answer.indexOf(",") <= 0) {
		if (givenAnswer.length > 1) {
		    return -1;
		} else {
		    if (givenAnswer[0].equals(answer)) {
			return 1;
		    } else {
			return -1;
		    }
		}
	    }
	    else {
		String[] answers = answer.split(",");
		if (answers.length < givenAnswer.length) {
		    return -1;
		} else if (answers.length == givenAnswer.length) {
		    int counts = 0;
		    for (int i = 0; i < givenAnswer.length; i++) {
			for (int j = 0; j < answers.length; j++) {
			    if (givenAnswer[i].equals(answers[j])) {
				counts++;
			    }
			}
			if (counts - i <= 0) {
			    return -1;
			}
		    }
		    return 1;
		} else {
		    int counts = 0;
		    for (int i = 0; i < givenAnswer.length; i++) {
			for (int j = 0; j < answers.length; j++) {
			    if (givenAnswer[i].equals(answers[j])) {
				counts++;
			    }
			}
			if (counts - i <= 0) {
			    return -1;
			}
		    }
		    return 0;
		}
	    }
	} else {
	    return -1;
	}
    }
    
    public static void removeDuplicateWithOrder(List arlList) 
    {  
      Set set = new HashSet();  
      List newList = new ArrayList();  
      for (Iterator iter = arlList.iterator(); iter.hasNext(); )  
      {  
      Object element = iter.next();  
      if (set.add(element)) newList.add(element);  
      }  
      arlList.clear();  
      arlList.addAll(newList);  
    }

    public static void main(String args[]) {
	System.out.println(Utilities.isRight(new String[]{}, "a"));
    }
}
